package com.wing.wcabinet.service;

import com.wing.common.utils.HttpHelper;
import com.wing.wcabinet.model.entity.WcabPushMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CLICKMsgService {

    @Value("${click.wsUrl}")
    private String wsUrl;

    @Autowired
    private WcabPushMsgService wcabPushMsgService;


    public boolean pushMsg(String commandStr,Long businessId,String enterpriseCode,Long creator,String creatorName){
        Boolean bool=true;
        WcabPushMsg pushMsg=new WcabPushMsg();
        pushMsg.setRequest(commandStr);
        pushMsg.setEnterpriseCode(enterpriseCode);
        pushMsg.setBusinessId(businessId);
        pushMsg.setCreator(creator);
        pushMsg.setCreatorName(creatorName);
        pushMsg.setRequest(commandStr);
        String responseCode="";
        String response="";
        try{

            log.info("msg:{}",commandStr);
            String url=wsUrl + "?commandString="+ commandStr;
            log.info("wsUrl:{}",url);
            String result= HttpHelper.httpGet(url);
            log.info("result:{}",result);

            if (!result.contains(">OK<")){
                log.info("push fail",result);
                bool=false;
                try{
                    responseCode=result.substring(result.lastIndexOf("\">")+2,result.lastIndexOf("</"));
                }catch (Exception e){
                    log.error("get response code fail",e);
                }
            }else{
                responseCode="OK";
            }
            response=result;
        }catch (Exception e){
            log.error("push click msg fail",e);
            bool=false;
            responseCode="exception";
            response=e.getMessage();

        }
        pushMsg.setResponse(response);
        pushMsg.setResponseCode(responseCode);
        wcabPushMsgService.save(pushMsg);
        return bool;
    }

}
